package com.badlogic.androidgames.jumper;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.gl.Animation;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.gl.TextureRegion;
import com.badlogic.androidgames.framework.impl.GLGraphics;

public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 10;
	static final float FRUSTUM_HEIGHT = 15;
	World world;
	SpriteBatcher batcher;
	GLGraphics glGraphics;
	Camera2D guiCam;
	
	public WorldRenderer( GLGraphics glGraphics,SpriteBatcher batcher, World world ) {		
		this.world = world;
		this.batcher = batcher;
		this.glGraphics = glGraphics;
		this.guiCam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
	}

	public void render() {
		// TODO Auto-generated method stub
		if(world.bob.position.y > guiCam.position.y)
			guiCam.position.y = world.bob.position.y;
		guiCam.setViewportAndMatrices();
		renderBackground();
		renderObjects();
		
		
	}

	private void renderBackground() {
		// TODO Auto-generated method stub
		batcher.beginBatch(Assets.background);
		batcher.drawSprite(guiCam.position.x, guiCam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion);
		batcher.endBatch();
		
	}

	private void renderObjects() {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGl();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		batcher.beginBatch(Assets.items);
		renderBob();
		renderPlatforms();
		renderItems();
		renderSquirrels();
		renderCastle();
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
		
	}

	private void renderBob() {
		// TODO Auto-generated method stub
		TextureRegion keyFrame;
		switch(world.bob.state){
			case Bob.BOB_STATE_FALL:		
				keyFrame = Assets.bobFall.getKeyFrame(world.bob.stateTime,Animation.ANIMATION_LOOPING);
				break;
			case Bob.BOB_STATE_JUMP:
				keyFrame = Assets.bobJump.getKeyFrame(world.bob.stateTime, Animation.ANIMATION_LOOPING);
				break;
			case Bob.BOB_STATE_HIT:
			default:
				keyFrame = Assets.bobHit;			

		}
		float side = world.bob.velocity.x < 0? -1: 1; // calcolo quale lato bob sta guardando
		batcher.drawSprite(world.bob.position.x, world.bob.position.y, 1 * side, 1, keyFrame); // 1*1-meter-to-32*32-pixel mapping
		
	}

	private void renderPlatforms() {
		// TODO Auto-generated method stub
		int len = world.platforms.size();
		for(int i = 0; i < len; i++){
			Platform platform = world.platforms.get(i);
			TextureRegion keyFrame = Assets.platform;
			if(platform.state == Platform.PLATFORM_STATE_PULVERIZING){
				keyFrame = Assets.brakingPlatform.getKeyFrame(platform.state, Animation.ANIMATION_NONLOOPING);
			}
			batcher.drawSprite(platform.position.x, platform.position.y, 2, 0.5f, keyFrame);
		}
		
	}

	private void renderItems() {
		// TODO Auto-generated method stub
		int len = world.springs.size();
		for(int i = 0; i < len; i++){
			Spring spring = world.springs.get(i);
			batcher.drawSprite(spring.position.x, spring.position.y, 1, 1, Assets.spring);
					
		}
		len = world.coins.size();
		for(int i = 0; i < len; i++){
			Coin coin = world.coins.get(i);
			TextureRegion keyFrame = Assets.coinAnim.getKeyFrame(coin.stateTime, Animation.ANIMATION_LOOPING);
			batcher.drawSprite(coin.position.x,coin.position.y, 1, 1, keyFrame);
		}
		
	}

	private void renderSquirrels() {
		// TODO Auto-generated method stub
		int len = world.squirrels.size();
		for(int i = 0; i < len; i++) {
			Squirrel squirrel = world.squirrels.get(i);
			TextureRegion keyFrame = Assets.squirrelFly.getKeyFrame(squirrel.stateTime, Animation.ANIMATION_LOOPING);
			float side = squirrel.velocity.x < 0?-1:1;
			batcher.drawSprite(squirrel.position.x, squirrel.position.y, side * 1, 1,keyFrame);
		}
		
	}

	private void renderCastle() {
		// TODO Auto-generated method stub
		Castle castle = world.castle;
		batcher.drawSprite(castle.position.x, castle.position.y, 2, 2, Assets.castle);
	}
	
	

}
